//
//  TTOpenShopViewController.m
//  TaoTuo
//
//  Created by admin on 2019/11/5.
//  Copyright © 2019 admin. All rights reserved.
//

#import "TTOpenShopViewController.h"
#import "TTOpenShopCollectionViewCell.h"
#import "TTOpenShopCollectionReusableView.h"
#import "TTOpenPlusPopView.h"
#import <UIView+TYAlertView.h>
#import "TTZFBPayTool.h"
#import "TTOpenPlusSuccessViewController.h"

static NSString *cellID = @"cellID";
static NSString *cellHeaderID = @"cell_header_id";

@implementation TTOpenShopModel

+ (instancetype)setModelWithImageName:(NSString *)imageName title:(NSString *)title subTitle:(nonnull NSString *)subTitle
{
    TTOpenShopModel *model = [[self alloc] init];
    model.imageName = imageName;
    model.title = title;
    model.subTitle = subTitle;
    return model;
}

@end

@interface TTOpenShopViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIImageView *topImageView;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, strong) UIImage *shadowImage;
///是否是续费
@property (nonatomic, assign) BOOL renewal;

@end

@implementation TTOpenShopViewController

- (instancetype)initWithRenewal:(BOOL)renewal
{
    self = [super init];
    if (self) {
        self.renewal = renewal;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.fd_interactivePopDisabled = YES;
    self.fd_prefersNavigationBarHidden = YES;
    self.title = @"我要开店";
    [self setupSubviews];
    [self setupDatas];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(openPlusDianpuSuccessNotify:) name:openDianpuSuccessNotifyKey object:nil];
    
}

- (void)backAction
{
    if (self.renewal) {
        [self dismissViewControllerAnimated:YES completion:nil];
    } else {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

// 店铺开通成功
- (void)openPlusDianpuSuccessNotify:(NSNotification *)notify
{
    // 请求vip接口，获取店铺地址
    [TTCommonNetData getVipLevelSuccess:^(TTUserModel * _Nonnull userModel) {} failure:^{}];
    // 设置为商家
    [TTLoginSingle sharedSingle].loginModel.VipLevel = @"1";
    [TTLoginSingle sharedSingle].userModel.VipLevel = 1;
    [LLPasswordManagerUtilies saveLoginUserInfoWithModel:[TTLoginSingle sharedSingle].loginModel];
    // 我的页面刷新
    [[TTLoginSingle sharedSingle].loginSubject sendNext:@1];
    
    TTOpenPlusSuccessViewController *openSuccessVC = [[TTOpenPlusSuccessViewController alloc] init];
    [self.navigationController pushViewController:openSuccessVC animated:YES];
}

// 开通店铺
- (void)openShopBtnClick
{
    TTOpenPlusPopView *popView = [TTOpenPlusPopView createViewFromNib];
    popView.gotoPaymentBlock = ^(NSString * _Nonnull money, NSString * _Nonnull year) {
        // 去支付
        [TTZFBPayTool gotoOpenPlusPayWithMoney:money year:year isRenewal:NO notifyKey:openDianpuSuccessNotifyKey];
    };
    TYAlertController *alertController = [TYAlertController alertControllerWithAlertView:popView preferredStyle:TYAlertControllerStyleActionSheet];
    alertController.backgoundTapDismissEnable = YES;
    [self presentViewController:alertController animated:YES completion:nil];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    TTOpenShopCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    cell.model = self.dataArray[indexPath.row];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(self.view.width, 70);
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableView = nil;
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        TTOpenShopCollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:cellHeaderID forIndexPath:indexPath];
        reusableView = view;
    }
    
    return  reusableView;
}

- (void)setupSubviews
{
    [self topImageView];
    [self setupBottomView];
    
    [self addNavBarView];
}

- (void)addNavBarView
{
    UIView *navBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, StatusBarAndNavBar_Height)];
    [self.view addSubview:navBarView];
    
    UIImage *image = [UIImage imageNamed:@"white_navback"];
    UIButton *backBtn = [UIButton createButtonWithImage:image target:self action:@selector(backAction)];
    [navBarView addSubview:backBtn];
    [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(kStatusBarHeight);
        make.left.equalTo(navBarView);
        make.width.height.mas_equalTo(44);
    }];
    
    UILabel *titleLabel = [UILabel labelWithTitle:@"我要开店" color:[UIColor whiteColor] font:[UIFont boldSystemFontOfSize:18]];
    [navBarView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(navBarView);
        make.top.equalTo(backBtn);
        make.height.mas_equalTo(44);
    }];
    
}

- (void)setupDatas
{
    [self.dataArray removeAllObjects];
    
    [self.dataArray addObject:[TTOpenShopModel setModelWithImageName:@"pinpaishangcheng" title:@"专属品牌商城" subTitle:@"一键生成多样式商城"]];
    [self.dataArray addObject:[TTOpenShopModel setModelWithImageName:@"luoshikucun" title:@"50万+裸石库存" subTitle:@"专属采购为您挑货"]];
    [self.dataArray addObject:[TTOpenShopModel setModelWithImageName:@"diaojiebeilv" title:@"自主调节倍率" subTitle:@"成本价/批发价自主设置"]];
    [self.dataArray addObject:[TTOpenShopModel setModelWithImageName:@"wumenkanyouhuiquan" title:@"无门槛优惠券" subTitle:@"全年240张 每件优惠6元"]];
    [self.dataArray addObject:[TTOpenShopModel setModelWithImageName:@"shuiyin" title:@"自设商标水印" subTitle:@"独立拥有自己的商标名称"]];
    [self.dataArray addObject:[TTOpenShopModel setModelWithImageName:@"yuming" title:@"绑定自有域名" subTitle:@"独立拥有自己的域名品牌"]];
    
    [self.collectionView reloadData];
    
    [self.view layoutIfNeeded];
    
    self.scrollView.contentSize = CGSizeMake(self.view.width, self.collectionView.bottom + 10);
    
}

- (void)setupBottomView
{
    NSString *title = self.renewal ? @"店铺续费" : @"开通店铺";
    UIButton *bottomBtn = [UIButton createButtonWithTitle:title titleColor:[UIColor whiteColor] titleFont:[UIFont systemFontOfSize:20] backgroundColor:UIColorHex(E2B762) target:self action:@selector(openShopBtnClick)];
//    [bottomBtn addGradientRampWithColors:@[UIColorHex(E2B762), UIColorHex(E4B664)] colorDirection:HorizontalDirection];
    [self.view addSubview:bottomBtn];
    [bottomBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.mas_bottomLayoutGuideTop);
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(55);
    }];
}

- (UIScrollView *)scrollView
{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.backgroundColor = [UIColor whiteColor];
        _scrollView.showsVerticalScrollIndicator = NO;
        [self.view addSubview:_scrollView];
        [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.equalTo(self.view);
            make.bottom.equalTo(self.mas_bottomLayoutGuideTop).mas_offset(-55);
        }];
    }
    return _scrollView;
}

- (UIImageView *)topImageView
{
    if (!_topImageView) {
        _topImageView = [[UIImageView alloc] init];
        _topImageView.image = [UIImage imageNamed:@"opentop"];
        _topImageView.contentMode = UIViewContentModeScaleToFill;
        [self.scrollView addSubview:_topImageView];
        [_topImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view);
            make.left.right.equalTo(self.view);
            make.height.mas_equalTo(227 + kStatusBarHeight);
        }];
    }
    return _topImageView;
}

- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        CGFloat w = floor((self.view.width - 49) * 0.5);
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake(w, 120);
        layout.minimumLineSpacing = 10;
        layout.minimumInteritemSpacing = 1;
        layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.scrollEnabled = NO;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
        [_collectionView registerClass:[TTOpenShopCollectionViewCell class] forCellWithReuseIdentifier:cellID];
        [_collectionView registerClass:[TTOpenShopCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:cellHeaderID];
        [self.scrollView addSubview:_collectionView];
        _collectionView.layer.cornerRadius = 25;
        _collectionView.layer.borderColor = UIColorHex(E6B962).CGColor;
        _collectionView.layer.borderWidth = 1;
        CGFloat offsetY = kStatusBarHeight == 20 ? 0 : kStatusBarHeight;
        [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(194 + offsetY);
            make.left.mas_equalTo(24);
            make.centerX.equalTo(self.view);
            make.height.mas_equalTo(458);
        }];
    }
    return _collectionView;
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
